package easy.trace.agent.support;


import java.util.List;
import java.util.Map;

import easy.trace.Span;
import easy.trace.agent.CollectorService;
import easy.trace.agent.RegisterService;
import easy.trace.dubbomonitor.HydraService;
import easy.trace.dubbomonitor.LeaderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TraceService implements RegisterService, CollectorService {
    private static final Logger logger = LoggerFactory.getLogger(TraceService.class);
    private LeaderService leaderService;
    private HydraService hydraService;
    private Map<String, String> registerInfo;
    public static final String SEED = "seed";
    private boolean isRegister = false;

    public TraceService() {
    }

    public boolean isRegister() {
        return this.isRegister;
    }

    @Override
    public void sendSpan(List<Span> spanList) {
        try {
            this.hydraService.push(spanList);
        } catch (Exception var3) {
            logger.error("hydraService 对象:{}", this.hydraService);
            logger.error("spanList size 大小:{}", spanList.size());
            var3.printStackTrace();
            logger.error(var3.fillInStackTrace().toString());
            logger.error(var3.getLocalizedMessage());
            logger.error(var3.getMessage());
            logger.warn("Trace data push failure~");
        }

    }

    @Override
    public boolean registerService(String name, List<String> services) {
        try {
            this.registerInfo = this.leaderService.registerClient(name, services);
        } catch (Exception var4) {
            logger.warn(var4.getMessage());
            logger.warn("[Hydra] Client global config-info cannot regist into the hydra system");
        }

        if (this.registerInfo != null) {
            logger.info("[Hydra] Global registry option is ok!");
            this.isRegister = true;
        }

        return this.isRegister;
    }

    @Override
    public boolean registerService(String appName, String serviceName, Integer type) {
        logger.info(appName + " " + serviceName);
        String serviceId = null;

        try {
            serviceId = this.leaderService.registerClient(appName, serviceName, type);
        } catch (Exception var6) {
            logger.error(var6.getMessage());
            logger.warn("[Hydra] client cannot regist service <" + serviceName + "> into the hydra system");
        }

        if (serviceId != null) {
            logger.info("[Hydra] Registry [" + serviceName + "] option is ok!");
            this.registerInfo.put(serviceName, serviceId);
            return true;
        } else {
            return false;
        }
    }

    public LeaderService getLeaderService() {
        return this.leaderService;
    }

    public void setLeaderService(LeaderService leaderService) {
        this.leaderService = leaderService;
    }

    public HydraService getHydraService() {
        return this.hydraService;
    }

    public void setHydraService(HydraService hydraService) {
        this.hydraService = hydraService;
    }

    public String getServiceId(String service, Integer type) {
        return this.isRegister && this.registerInfo.containsKey(service) ? (String)this.registerInfo.get(service) : null;
    }

    public Long getSeed() {
        String s = null;
        if (this.isRegister) {
            s = (String)this.registerInfo.get("seed");
            return Long.valueOf(s);
        } else {
            return null;
        }
    }
}

